#include <my_head.h>

int value2 = 0;

void *task_func(void *arg){
    printf("我是子线程:[%ld] 10秒后退出\n", pthread_self());
    sleep(2);
    printf("我是子线程:[%ld] 退出\n", pthread_self());
    // 错误 
    // int value1 = 123;
    // pthread_exit(&value1);
    
    // 正确
    // value2 = 456;
    // pthread_exit(&value2);

    // 正确
    // static int value3 = 789;
    // pthread_exit(&value3);

    // 正确
    int *q = (int *)malloc(sizeof(int));
    *q = 11223344;
    pthread_exit(q);
}

int main(int argc, const char *argv[]){
    pthread_t tid = 0;
    int ret = 0;
    if(0 != (ret = pthread_create(&tid, NULL, task_func, NULL))){
        printf("pthread_create error : [%d] [%s]\n", ret, strerror(ret));
        exit(EXIT_FAILURE);
    }
    printf("我是主线程:[%ld] 等待子线程结束 为其回收资源\n", pthread_self());
    int *p = NULL;
    if(0 != (ret = pthread_join(tid, (void **)&p))){ // 阻塞等待
        printf("pthread_join error : [%d] [%s]\n", ret, strerror(ret));
    }
    printf("我是主线程:[%ld] 给子线程回收资源成功 子线程退出的状态值:[%d]\n", \
                            pthread_self(), *p);
    free(p);
    sleep(100);
    return 0;
}